package com.example.service_order.controller;

import com.example.common.commonsutils.R;
import com.example.service_order.entity.vo.OrderVO;
import com.example.service_order.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @Author:tjf
 * @Description:
 */
@RestController
@RequestMapping("/orderservice")
@Tag(name = "订单控制器")
public class OrderController {
    @Resource
    private OrderService orderService;
    @Operation(description = "创建课程订单")
    @PostMapping("/{courseId}")
    public R createCourseOrder(@PathVariable("courseId") String courseId,
                               @RequestHeader("token") String token){
        String orderNo = orderService.saveOrder(courseId, token);
        return R.ok().data("orderNo", orderNo);
    }

    @Operation(description = "根据订单编号，查询订单")
    @GetMapping("/{orderNo}")
    public R queryCourseOrderByNo(@PathVariable("orderNo") String orderNo){
        OrderVO orderVO = orderService.queryCourseOrder(orderNo);
        return R.ok().data("order", orderVO);
    }
    @Operation(description = "查询课程是否已经购买")
    @GetMapping("/coursestate/{courseId}")
    public R queryCourseIsPurchase(@PathVariable("courseId") String courseId,
                                   @RequestHeader(value = "token", required = false) String token){
        if (StringUtils.isBlank(token)){
            return R.ok().data("isBuy", false);
        }
        boolean isBuy = orderService.queryCourseIsPurchase(courseId, token);
        return R.ok().data("isBuy", isBuy);
    }
}
